package com.erpanderp.wam.usm.model.service.leave;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.usm.model.entity.leave.LeaveBalance;
import com.erpanderp.wam.usm.model.entity.leave.LeaveBalanceTracker;
import com.erpanderp.wam.util.HibernateUtil;

public class LeaveBalanceTrackerDAO {

	
	public List<LeaveBalanceTracker> getLeaveBalanceTracker(Long employeeId,int month, int year, int day){
		System.out.println("IN GETTING LEAVE BALANCE TRACKERS METHOD");
		List<LeaveBalanceTracker> leaveBalanceTrackers;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			String queryString = "from LeaveBalanceTracker lbt where lbt.employee.employeeId=:employeeId and lbt.month=:month and lbt.year=:year and lbt.day=:day";
			Query query = session.createQuery(queryString).setLong("employeeId", employeeId).setInteger("month", month).setInteger("year", year).setInteger("day", day);
			leaveBalanceTrackers = (List<LeaveBalanceTracker>)query.list();
			return leaveBalanceTrackers;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
	}
	public List<LeaveBalanceTracker> isNewJoinee(Long employeeId){
		System.out.println("IN GETTING LEAVE BALANCE TRACKERS METHOD");
		List<LeaveBalanceTracker> leaveBalanceTrackers;
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			String queryString = "from LeaveBalanceTracker lbt where lbt.employee.employeeId=:employeeId";
			Query query = session.createQuery(queryString).setLong("employeeId", employeeId);
			leaveBalanceTrackers = (List<LeaveBalanceTracker>)query.list();
			return leaveBalanceTrackers;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
			// TODO: handle exception
		}
	}
	
	public  List<LeaveBalanceTracker> createLeaveBalanceTrackers(List<LeaveBalanceTracker> leaveBalanceTrackers){
		System.out.println("LEAVE BALANCES TRACKERS SIZE IS:"+leaveBalanceTrackers.size());
		System.out.println("LEAVES TRACKER CREATION IN PROGRESS");
		try{
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.AUTO);
			for(LeaveBalanceTracker leaveBalanceTracker : leaveBalanceTrackers){
				session.save(leaveBalanceTracker);
			}
			session.getTransaction().commit();
		}catch (Exception e) {
			e.printStackTrace();
			
			// TODO: handle exception
		}
		return leaveBalanceTrackers;
		
	}
}
